/**
 * Created by deng on 16-3-15.
 */


angular.module('metalabPartnerApp').controller('TalentEditCtrl', ['$scope', '$location', '$stateParams', 'MetaResources', 'MetaRemind', 'MetaSEO'
    , 'talent', 'MetaConstants'
    , function ($scope, $location, $stateParams, MetaResources, MetaRemind, MetaSEO, talent, MetaConstants) {

        $scope.index = 0;
        $scope.temp = {};
        $scope.editing = {};

        //qnuploader 配置
        $scope.uploaderConfig = CONFIG.qnuploader;

        //研究方向和研究成果的展示存储
        $scope.researchAreas, $scope.projectData, $scope.paperData, $scope.patentData = [];

        /**
         * Config 学科类别 赋值
         */
        $scope.disciplineClasses = MetaConstants.disciplineClasses;
        $scope.disciplines = MetaConstants.disciplines;

        /**
         * talent赋值
         */

        $scope.talent = talent;
        $scope.user = talent.user;

        /**
         * 从后台获取researchAreas
         */
        MetaResources.researchArea.query({talentId: talent.id}, function (data) {
            $scope.researchAreas = data;
        });

        /**
         * 从后台获取项目
         */
        MetaResources.achievement.query({talentId: talent.id, 'tag': '项目'}, function (data) {
            $scope.projectData = data;
        });
        /**
         * 从后台获取专利：patentData
         */
        MetaResources.achievement.query({talentId: talent.id, 'tag': '专利'}, function (data) {
            $scope.patentData = data;
        });
        /**
         * 从后台获取论文：paperData
         */
        MetaResources.achievement.query({talentId: talent.id, 'tag': '论文'}, function (data) {
            $scope.paperData = data;
        });

        $scope.removeKeyword = function (index) {
            $scope.talent.keywords.splice(index, 1);
        };

        $scope.talentUpdate = function () {
            var updateData = angular.copy($scope.talent);
            delete updateData.user;
            delete updateData['user_id'];
            delete updateData.id;
            MetaResources.talent.update({userId: '_self'}, updateData, function (data) {
                $scope.talent = data;
                MetaRemind.success('修改成功');
            }, function (error) {
                MetaRemind.error(error);
            });
        };

        /**
         * researchArea 五种操作
         * 增加 add
         * 编辑 edit
         * 修改 update
         * 取消修改 cancel
         * 删除 delete
         */
        $scope.addResearchArea = function () {
            if ($scope.researchAreaNew && $scope.researchAreaNew.name && $scope.researchAreaNew.content) {
                //向后台增加数据，成功push进入talents.areas
                MetaResources.researchArea.create({talentId: talent.id}, $scope.researchAreaNew, function (data) {
                    MetaRemind.success('增加成功');
                    $scope.researchAreas.push(data);
                    $scope.researchAreaNew = {};
                }, function (error) {
                    MetaRemind.error(error);
                })

            } else {
                MetaRemind.error('请填写研究领域的标题和内容！');
            }
        };

        $scope.editResearchArea = function (area, index) {
            $scope.temp.areaUpdate = angular.copy(area);
            $scope.editing.areas = index;
        };

        $scope.updateResearchArea = function (id, index) {

            /**
             * 把tempArea中的内容发至后台，然后返回
             */
            if ($scope.temp.areaUpdate.name && $scope.temp.areaUpdate.content) {
                MetaResources.researchArea.update({id: id}, $scope.temp.areaUpdate, function (data) {
                    MetaRemind.success('修改成功');
                    $scope.researchAreas[index] = data;
                    $scope.cancelResearchArea();
                }, function (error) {
                    MetaRemind(error);
                })
            } else {
                MetaRemind.error('请填写研究领域的标题和内容!');
            }
        };

        $scope.cancelResearchArea = function () {
            $scope.editing.areas = -1;
            delete $scope.temp.areaUpdate;
        };

        $scope.deleteResearchArea = function (id, index) {
            if (confirm('确认删除？')) {
                MetaResources.researchArea.delete({id: id}, function (data) {
                    MetaRemind.success('删除成功');
                    $scope.researchAreas.splice(index, 1);
                }, function (error) {
                    MetaRemind.error(error);
                });
            }
        };

        /**
         * project 五种操作
         * 增加 add
         * 编辑 edit
         * 修改 update
         * 取消修改 cancel
         * 删除 delete
         */
        $scope.addProject = function () {
            if ($scope.projectNew && $scope.projectNew.title) {
                var achievement = {
                    title: $scope.projectNew.title,
                    content: $scope.projectNew.content,
                    attachments: $scope.projectNew.attachments,
                    tag: '项目'
                };
                utils.addAchievement(talent.id, achievement, function (data) {
                    $scope.projectData.push(data);
                    $scope.projectNew = {};
                }, function (error) {
                    MetaRemind.error(error);
                });
            } else {
                MetaRemind.error('请填写项目的名称');
            }
        };

        $scope.editProject = function (project, index) {
            $scope.temp.projectUpdate = angular.copy(project);
            $scope.editing.projects = index;
        };

        $scope.updateProject = function (id, index) {
            if ($scope.temp.projectUpdate.title) {
                utils.updateAchievement(id, $scope.temp.projectUpdate, function (data) {
                    $scope.projectData[index] = data;
                    $scope.cancelProject();
                });
            } else {
                MetaRemind.error('请填写项目的标题');
            }
        };

        $scope.cancelProject = function () {
            $scope.editing.projects = -1;
            delete $scope.temp.projectUpdate;
        };

        $scope.deleteProject = function (id, index) {
            if (confirm('确认删除？')) {
                utils.deleteAchievement(id, function (data) {
                    $scope.projectData.splice(index, 1);
                });
            }
        };

        /**
         * patent 五种操作
         * 增加 add
         * 编辑 edit
         * 修改 update
         * 取消修改 cancel
         * 删除 delete
         */
        $scope.addPatent = function () {
            if ($scope.patentNew && $scope.patentNew.title) {
                var achievement = {
                    title: $scope.patentNew.title,
                    content: $scope.patentNew.content,
                    attachments: $scope.patentNew.attachments,
                    tag: '专利'
                };
                utils.addAchievement(talent.id, achievement, function (data) {
                    $scope.patentData.push(data);
                    $scope.patentNew = {};
                }, function (error) {
                    MetaRemind.error(error);
                });
            } else {
                MetaRemind.error('请填写专利的名称');
            }
        };

        $scope.editPatent = function (patent, index) {
            $scope.temp.patentUpdate = angular.copy(patent);
            $scope.editing.patents = index;
        };

        $scope.updatePatent = function (id, index) {
            if ($scope.temp.patentUpdate.title) {
                utils.updateAchievement(id, $scope.temp.patentUpdate, function (data) {
                    $scope.patentData[index] = data;
                    $scope.cancelPatent();
                });
            } else {
                MetaRemind.error('请填写项目的标题');
            }
        };

        $scope.cancelPatent = function () {
            $scope.editing.patents = -1;
            delete $scope.temp.patentUpdate;
        };

        $scope.deletePatent = function (id, index) {
            if (confirm('确认删除？')) {
                utils.deleteAchievement(id, function (data) {
                    $scope.patentData.splice(index, 1);
                });
            }
        };

        /**
         * paper 五种操作
         * 增加 add
         * 编辑 edit
         * 修改 update
         * 取消修改 cancel
         * 删除 delete
         */
        $scope.addPaper = function () {
            if ($scope.paperNew && $scope.paperNew.title) {
                var achievement = {
                    title: $scope.paperNew.title,
                    content: $scope.paperNew.content,
                    attachments: $scope.paperNew.attachments,
                    tag: '论文'
                };
                utils.addAchievement(talent.id, achievement, function (data) {
                    $scope.paperData.push(data);
                    $scope.paperNew = {};
                }, function (error) {
                    MetaRemind.error(error);
                });
            } else {
                MetaRemind.error('请填写论文的名称');
            }

        };

        $scope.editPaper = function (paper, index) {
            $scope.temp.paperUpdate = angular.copy(paper);
            $scope.editing.papers = index;
        };

        $scope.updatePaper = function (id, index) {
            if ($scope.temp.paperUpdate.title) {
                utils.updateAchievement(id, $scope.temp.paperUpdate, function (data) {
                    $scope.paperData[index] = data;
                    $scope.cancelPaper();
                });
            } else {
                MetaRemind.error('请填写项目的标题');
            }
        };

        $scope.cancelPaper = function () {
            $scope.editing.papers = -1;
            delete $scope.temp.paperUpdate;
        };

        $scope.deletePaper = function (id, index) {
            if (confirm('确认删除？')) {
                utils.deleteAchievement(id, function (data) {
                    $scope.paperData.splice(index, 1);
                });
            }
        };

        /**
         * utils
         * 3个方法，achievement增删改
         * @type {{}}
         */

        var utils = {};

        utils.addAchievement = function (talentId, achievement, successF, errorF) {
            MetaResources.achievement.create({talentId: talentId}, achievement, function (data) {
                MetaRemind.success('添加成功');
                successF(data);
            }, function (error) {
                errorF(error);
            })
        };

        utils.updateAchievement = function (id, achievement, successF, errorF) {
            MetaResources.achievement.update({id: id}, achievement, function (data) {
                MetaRemind.success('修改成功');
                successF(data);
            }, function (error) {
                MetaRemind(error);
            })
        };

        utils.deleteAchievement = function (id, successF, errorF) {
            MetaResources.achievement.delete({id: id}, function (data) {
                MetaRemind.success('删除成功');
                successF(data);
            }, function (error) {
                MetaRemind(error);
            })
        }
    }
])
;
